using Blazing.BusinessObjects.Cms;
using System.BusinessObjects.Providers;
using NHibernate;
using System.BusinessObjects.Transactions;
using NHibernate.Classic;
using Blazing.BusinessObjects.Widgets;
using System.Collections;
using System.BusinessObjects.Data;

using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using ClassInitialize = NUnit.Framework.TestFixtureSetUpAttribute;
using ClassCleanup = NUnit.Framework.TestFixtureTearDownAttribute;

namespace BusinessObjectTests
{
    [TestClass]
    public class ContentPageTests : NHibernateInMemoryTestFixtureBase
    {

        private void CheckValues(Contentpage obj, Contentpage newObj)
        {
            Assert.AreEqual(obj.ID, newObj.ID);
            Assert.AreEqual(obj.Content, newObj.Content);
            Assert.AreEqual(obj.Comments, newObj.Comments);
            Assert.AreEqual(obj.IsActive, newObj.IsActive);
            Assert.AreEqual(obj.IsHttps, newObj.IsHttps);
            Assert.AreEqual(obj.Name, newObj.Name);
            Assert.AreEqual(obj.PageTitle, newObj.PageTitle);

            Assert.AreEqual(obj.PageType.ID, newObj.PageType.ID);
            Assert.AreEqual(obj.PageType.Description, newObj.PageType.Description);
            Assert.AreEqual(obj.PageType.Name, newObj.PageType.Name);
            Assert.AreEqual(obj.PageType.IsCmsPage, newObj.PageType.IsCmsPage);

            Assert.AreEqual(obj.Masterpage.ID, newObj.Masterpage.ID);
            Assert.AreEqual(obj.Masterpage.Description, newObj.Masterpage.Description);
            Assert.AreEqual(obj.Masterpage.Name, newObj.Masterpage.Name);
            Assert.AreEqual(obj.Masterpage.Path, newObj.Masterpage.Path);

            Assert.AreEqual(obj.Theme.ID, newObj.Theme.ID);
            Assert.AreEqual(obj.Theme.Description, newObj.Theme.Description);
            Assert.AreEqual(obj.Theme.Name, newObj.Theme.Name);
            Assert.AreEqual(obj.Theme.Path, newObj.Theme.Path);
            Assert.AreEqual(obj.CreatedDate, newObj.CreatedDate);
            Assert.IsFalse(obj.IsNull("CreatedDate"));
        }

        [TestMethod]
        public void CreateContentPage()
        {
            Contentpage content = BusinessObjectFactory.CreateAndFillContentPage();
            Masterpage mPage = BusinessObjectFactory.CreateAndFillMasterPage();
            mPage.Site = BusinessObjectFactory.CreateAndFillSite();
            mPage.Site.Save();
            mPage.Save();
            PageType pType = BusinessObjectFactory.CreateAndFillPageType();
            pType.Save();
            Theme theme = BusinessObjectFactory.CreateAndFillTheme();
            theme.Masterpage = mPage;
            theme.Save();

            Widget wid = BusinessObjectFactory.CreateAndFillWidget();
            wid.Site = mPage.Site;

            PageWidget pw = new PageWidget();
            pw.Widget = wid;
            pw.Page = content;
            pw.Region = BusinessObjectFactory.CreateAndFillWidgetRegion();
            pw.Region.Save();

            content.Widgets.Add(pw);

            content.PageType = pType;
            content.Masterpage = mPage;
            content.Theme = theme;
            content.Site = mPage.Site;

            content.Save();

            Contentpage content2 = Contentpage.Load(content.ID);
            Assert.IsNotNull(content2);
            CheckValues(content, content2);
        }

        [TestMethod]
        [ExpectedException(typeof(ValidationFailure))]
        public void ContentPageCantBeOwnParent()
        {
            Contentpage content = BusinessObjectFactory.CreateAndFillContentPage();
            Masterpage mPage = BusinessObjectFactory.CreateAndFillMasterPage();
            mPage.Site = BusinessObjectFactory.CreateAndFillSite();
            mPage.Site.Save();
            mPage.Save(SaveMode.Flush);
            PageType pType = BusinessObjectFactory.CreateAndFillPageType();
            pType.Save();
            Theme theme = BusinessObjectFactory.CreateAndFillTheme();
            theme.Masterpage = mPage;
            theme.Save(SaveMode.Flush);

            content.PageType = pType;
            content.Masterpage = mPage;
            content.Theme = theme;
            content.Site = mPage.Site;
            content.Save(SaveMode.Flush);

            using (UnitOfWork.Begin())
            {
                content.ParentPage = content;
                content.Save();
            }

            using (UnitOfWork.Begin())
            {
                content.Delete();
                content.Save();
                mPage.Delete();
                mPage.Save();
                pType.Delete();
                pType.Save();
                theme.Delete();
                theme.Save();
            }

        }

        [TestMethod]
        public void CreateFeedbackItem()
        {
            Contentpage content = BusinessObjectFactory.CreateAndFillContentPage();
            Masterpage mPage = BusinessObjectFactory.CreateAndFillMasterPage();
            mPage.Site = BusinessObjectFactory.CreateAndFillSite();
            mPage.Site.Save();
            mPage.Save();
            PageType pType = BusinessObjectFactory.CreateAndFillPageType();
            pType.Save();
            Theme theme = BusinessObjectFactory.CreateAndFillTheme();
            theme.Masterpage = mPage;
            theme.Save();

            content.PageType = pType;
            content.Masterpage = mPage;
            content.Theme = theme;
            content.Site = mPage.Site;

            content.Save();

            FeedbackItem item = BusinessObjectFactory.CreateAndFillFeedbackItem();
            content.FeedbackItems.Add(item);
            content.Save(SaveMode.Flush);

            int contentId = content.ID;
            Contentpage.Evict(contentId);

            Contentpage loadedPage = Contentpage.Load(content.ID);
            Assert.IsTrue(loadedPage.FeedbackItems.Count > 0);
        }
    }
}
